package com.yuyou.tas.feedback.service.impl;

import com.yuyou.tas.feedback.IWeixinFIleService;
import com.yuyou.tas.feedback.mapper.FeedbackMapper;
import com.yuyou.tas.feedback.service.*;
import com.yuyou.tas.feedback.vo.resp.FeedbackClassStatis;
import com.yuyou.tas.feedback.vo.resp.FeedbackCommentStatis;
import com.yuyou.tas.feedback.vo.resp.FeedbackSituationStatis;
import com.yuyou.tas.feedback.vo.resp.FeedbackStudentStatis;
import com.yuyou.tas.service.api.admin.entity.CommJob;
import com.yuyou.tas.service.api.admin.service.CommJobAPI;
import com.yuyou.tas.service.api.feedback.dto.FeedbackListDTO;
import com.yuyou.tas.service.api.feedback.entity.*;
import com.yuyou.tas.service.api.feedback.vo.req.FeedBackEditReqVo;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackNameResp;
import com.yuyou.tas.service.api.feedback.vo.resp.StudentFeedBackEdit;
import com.yuyou.tas.service.api.lesson.service.LessonClassAPI;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.admin.CommJobEnums;
import com.yuyou.tas.util.enums.feedback.FeedbackCommentEnums;
import com.yuyou.tas.util.enums.feedback.FeedbackEnums;
import com.yuyou.tas.util.enums.feedback.FeedbackSituationEnums;
import com.yuyou.tas.util.enums.feedback.MediaOperateEnums;
import com.yuyou.tas.util.enums.lesson.LessonClassEnums;
import com.yuyou.tas.util.tool.BeanUtils;
import com.yuyou.tas.util.tool.DateUtil;
import com.yuyou.tas.util.tool.LogUtil;
import com.yuyou.tas.util.tool.Sequence;

import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FeedbackServiceImpl implements IFeedbackService {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final BigDecimal EXEC_NUM_PROPORTION = new BigDecimal(0.1);

    @Autowired
    private FeedbackMapper mapper;

    @Autowired
    private IFeedbackSituationService feedbackSituationService;

    @Autowired
    private IFeedbackClassService feedbackClassService;

    @Autowired
    private IFeedbackStudentExceService feedbackStudentExceService;

    @Autowired
    private IFeedbackCommentService feedbackCommentService;

    @Autowired
    private LessonClassAPI lessonClassAPI;

    @Autowired
    private IFeedbackNoteService feedbackNoteService;

    @Autowired
    private CommJobAPI commJobAPI;

    @Autowired
    private IWeixinFIleService weixinFIleService;

    @Autowired
    private ICallableService callableService;


    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Feedback record) {
        return mapper.insert(record);
    }

    @Override
    public Feedback selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Feedback record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int findFeedbackCount(Pagination<FeedbackListDTO> queryPgn) {
        return mapper.findFeedbackCount(queryPgn);
    }

    @Override
    public List<FeedbackListDTO> feedbackList(Pagination<FeedbackListDTO> queryPgn) {
        return mapper.findFeedbackList(queryPgn);
    }

    @Override
    @Transactional
    public ResBoolSimpleInfo<Feedback> addOrUpdateFeedbackService(FeedBackEditReqVo vo){
        Feedback feedback = null;
        if(vo != null && vo.getFeedbackClassId() != null){

            FeedbackClass feedbackClass = feedbackClassService.selectByPrimaryKey(vo.getFeedbackClassId());
            feedback = mapper.findFeedbackByStuFeedbackClass(vo.getReceUserId() , vo.getFeedbackClassId());
            if(feedbackClass != null) {

                //去掉优秀学生人数限制！
/*                //检查优秀学生人数是否超过
                if(vo.getIs_exce() != null && vo.getIs_exce()){
                    boolean re = checkExecNum(feedbackClass , vo.getReceUserId());
                    if(!re){
                        return new ResBoolSimpleInfo<>(false , "设置的优秀学生数已超班级的10%。" , feedback);
                    }
                }*/

                if (feedback != null) {//更新操作
                    feedback.setStatus(vo.getStatus());
                    feedback.setIsRead(0);
                    mapper.updateByPrimaryKeySelective(feedback);
                    //删除学习情况
                    feedbackSituationService.delByFeedId(feedback.getId());
                    //删除优秀学生记录
                    feedbackStudentExceService.delByStudentFeedClass(feedbackClass.getId() , vo.getReceUserId());
                } else {//新增操作
                    feedback = new Feedback();
                    //创建反馈数据
                    feedback.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
                    feedback.setCreateTime(new Date());
                    feedback.setCreateUser(vo.getUserId());
                    feedback.setFeedbackClassId(feedbackClass.getId());
                    feedback.setStatus(vo.getStatus() == null ?FeedbackEnums.status.init.getValue():vo.getStatus());
                    feedback.setIsRead(0);
                    feedback.setReceUserId(vo.getReceUserId());
                    feedback.setId(Sequence.getSequence().nextId());

                    //获取班级反馈下的序号最大值
                    Integer sort = mapper.getMaxSortByFeedClass(feedbackClass.getId());
                    feedback.setSort(sort == null ? 1:sort+1);
                    mapper.insert(feedback);
                }

                //保存学习情况
                saveSituation(vo , feedback ,feedbackClass);

            }
        }else{
            return new ResBoolSimpleInfo<>(false , "参数错误，请联系管理员" , new Feedback());
        }
        return new ResBoolSimpleInfo<>(true , "" , feedback);
    }


    /**
     * 检查优秀学生人数
     * @param feedbackClass
     */
    public boolean checkExecNum(FeedbackClass feedbackClass , Long stuId){
           //该班级反馈的全部优秀学生
           List<FeedbackStudentExce> exces = feedbackStudentExceService.getByFeedbackClassId(feedbackClass.getId());

            for (FeedbackStudentExce feedbackStudentExce:
                    exces) {
                if(feedbackStudentExce.getStudentId().equals(stuId)){
                    return true;
                }

            }

            //班级人数
            int count = lessonClassAPI.getStudentCountByClassId(feedbackClass.getLessonClassId());

           //可设置人数----比例*总人数--向上取整
           int exec_count = new BigDecimal(count).multiply(EXEC_NUM_PROPORTION).setScale(0, BigDecimal.ROUND_UP).intValue();

           if(exces.size() >= exec_count){
               return false;
               //throw new BusinessException("设置的优秀学生数已超班级的10%。");
           }
           return true;
    }


    @Override
    public ResBoolSimpleInfo<String> batchPublish(Long feedbackClassId) {
        mapper.updateStatusByFeedClass(feedbackClassId , FeedbackEnums.status.published.getValue());
        return new ResBoolSimpleInfo(true , "");
    }

    @Override
    public List<StudentFeedBackEdit> findStuDetailByFeedClassId(Long feedbackClassId , Long classId) {
        return mapper.findStuDetailByFeedClassId(feedbackClassId , classId);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insert2Job(List<FeedbackSituation> feedbackSituations, List<FeedbackNote> feedbackNotes) {
        Date now = new Date();
        List<CommJob> jobs = new ArrayList<>();
        if(feedbackSituations != null && feedbackSituations.size() > 0){
            feedbackSituations.forEach(feedback -> {
                CommJob commJob = new CommJob();
                commJob.setId(Sequence.getSequence().nextId());
                commJob.setfId(feedback.getId());
                commJob.setStatus(CommJobEnums.status.init.getValue());
                commJob.setCreateTime(now);
                commJob.setInitTime(now);
                commJob.setTag(CommJobEnums.tag.feedbackSituation.getValue());
                commJob.setType(CommJobEnums.type.feedbackSituation.getValue());


                commJob.setHasTime(0);
                commJob.setNeedRetry(CommJobEnums.needRetry.getByType(commJob.getType()));
                commJob.setMaxTime(CommJobEnums.maxTime.getByType(commJob.getType()));

                jobs.add(commJob);

                feedback.setStatus(MediaOperateEnums.status.operating.getValue());
                feedbackSituationService.updateByPrimaryKeySelective(feedback);
            });
        }

        if(feedbackNotes != null && feedbackNotes.size() > 0){
            feedbackNotes.forEach(feedbackNote -> {
                CommJob commJob = new CommJob();
                commJob.setId(Sequence.getSequence().nextId());
                commJob.setfId(feedbackNote.getId());
                commJob.setStatus(CommJobEnums.status.init.getValue());
                commJob.setCreateTime(now);
                commJob.setInitTime(now);
                commJob.setTag(CommJobEnums.tag.feedbackNote.getValue());
                commJob.setType(CommJobEnums.type.feedbackNote.getValue());


                commJob.setHasTime(0);
                commJob.setNeedRetry(CommJobEnums.needRetry.getByType(commJob.getType()));
                commJob.setMaxTime(CommJobEnums.maxTime.getByType(commJob.getType()));

                jobs.add(commJob);

                feedbackNote.setStatus(MediaOperateEnums.status.operating.getValue());
                feedbackNoteService.updateByPrimaryKeySelective(feedbackNote);
            });
        }

        commJobAPI.insertJobs(jobs);
    }

    @Override
    public void AsynUploadJob(CommJob commJob) {
        if(CommJobEnums.type.feedbackSituation.getValue() == commJob.getType()){//图片音频
            FeedbackSituation f = feedbackSituationService.selectByPrimaryKey(commJob.getfId());
            if(f != null){
                String type = "image";
                if(FeedbackSituationEnums.type.voice.getValue() == f.getType()){
                    type = "voice";
                }
                try {
                    f.setMedia(weixinFIleService.upload2Qiniu(f.getWeixinMediaId() , type));
                } catch (Exception e) {
                    commJobAPI.updateJob(commJob.getId() , CommJobEnums.status.fail.getValue() , e.getMessage());
                    LogUtil.error(logger, "", e);
                    return;
                }

                commJobAPI.updateJob(commJob.getId() , CommJobEnums.status.success.getValue() , null);
                f.setStatus(MediaOperateEnums.status.success.getValue());
                f.setWeixinMediaId(null);
                feedbackSituationService.updateByPrimaryKeySelective(f);
            }else {
                commJobAPI.updateJob(commJob.getId() , CommJobEnums.status.cancel.getValue() , "FeedbackSituation数据查找不到");
            }
        }else if(CommJobEnums.type.feedbackNote.getValue() == commJob.getType()){//优秀学生笔记
            FeedbackNote feedbackNote = feedbackNoteService.selectByPrimaryKey(commJob.getfId());
            if(feedbackNote != null){
                try {
                    feedbackNote.setMedia(weixinFIleService.upload2Qiniu(feedbackNote.getWeixinMediaId() , "image"));
                } catch (Exception e) {
                    commJobAPI.updateJob(commJob.getId() , CommJobEnums.status.fail.getValue() , e.getMessage());
                    LogUtil.error(logger, "", e);
                    return;
                }

                commJobAPI.updateJob(commJob.getId() , CommJobEnums.status.success.getValue() , null);
                feedbackNote.setStatus(MediaOperateEnums.status.success.getValue());
                feedbackNote.setWeixinMediaId(null);
                feedbackNoteService.updateByPrimaryKeySelective(feedbackNote);
            }else {
                commJobAPI.updateJob(commJob.getId() , CommJobEnums.status.cancel.getValue() , "FeedbackNote数据查找不到");
            }

        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public HSSFWorkbook feedbackStatis(List<Long> baseSchoolIds, Long baseTrimesterId, List<Long> baseSectionIds, List<Long> dataSubjectIds) {
        HSSFWorkbook feedbackExcel = new HSSFWorkbook();

        HSSFCellStyle bodyStyle = feedbackExcel.createCellStyle();
        bodyStyle.setWrapText(true);

        bodyStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);//垂直
        bodyStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);//水平

        HSSFSheet sheet1 = feedbackExcel.createSheet("班级反馈");

        HSSFRow firstRow1 = sheet1.createRow(0);

        String[] titles1 = new String[] {"校区","期数","年级","班级编号","班级名称","进度状态","班级反馈总数","反馈标题","学生总数","初始化学生数量","草稿状态学生数量",
                "提交状态学生数量","跳过学生数量","是否完全提交","反馈日期","创建老师","任课老师","家长反馈总数"};

        for (int i = 0; i < titles1.length; i++) {
            HSSFCell cell = firstRow1.createCell(i);
            cell.setCellValue(titles1[i]);
        }
        List<FeedbackClassStatis> feedbackClassStatisList = mapper.feedbackOfClass(baseSchoolIds, baseTrimesterId, baseSectionIds, dataSubjectIds);
        // 学生数为 0 的，不导出
        feedbackClassStatisList.removeIf(f-> f.getStudentNum() == 0);

        for (int i = 0; i < feedbackClassStatisList.size(); i++ ) {

            FeedbackClassStatis fcs = feedbackClassStatisList.get(i);
            HSSFRow row = sheet1.createRow(i + 1);

            int c = 0;
            HSSFCell cell = row.createCell(c++);
            cell.setCellValue(fcs.getBaseSchool());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getBaseTrimester());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getBaseSection());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getLessonClassNum());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getLessonClassName());
            
            cell = row.createCell(c++);
            cell.setCellValue(LessonClassEnums.status.getNameByValue(fcs.getStatus()));

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getFeedbackClassNum());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getFeedbackClass());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getStudentNum());

            cell = row.createCell(c++);
            int initNum = fcs.getStudentNum() - fcs.getZeroStatusNum() - fcs.getOneStatusNum() - fcs.getTwoStatusNum();
            cell.setCellValue(initNum < 0 ? 0 : initNum);

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getZeroStatusNum());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getOneStatusNum());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getTwoStatusNum());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getStudentNum() > 0 && (fcs.getStudentNum() == fcs.getOneStatusNum()) ? "是" : "否");

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getCreateTime());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getCreateTeacher());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getLessonClassTeacher());

            cell = row.createCell(c++);
            cell.setCellValue(fcs.getParentReplies() == null ? 0:fcs.getParentReplies());
        }

        HSSFSheet sheet2 = feedbackExcel.createSheet("学生反馈");

        HSSFRow firstRow2 = sheet2.createRow(0);

        String[] titles2 = new String[] {"校区","期数","年级","班级编号","班级名称","进度状态","学生编号","学生","反馈标题","反馈总数","草稿状态总数","发布状态总数","跳过状态总数","反馈日期","创建老师","任课老师",
        		"回复内容","是否送花","是否优秀学生（是/否）","教师反馈标签","教师反馈内容","家长是否已读"};

        for (int i = 0; i < titles2.length; i++) {
            HSSFCell cell = firstRow2.createCell(i);
            cell.setCellValue(titles2[i]);
        }
        List<FeedbackStudentStatis> feedbackStudentStatisList = mapper.feedbackOfStudent(baseSchoolIds, baseTrimesterId, baseSectionIds, dataSubjectIds);

        List<FeedbackCommentStatis> feedbackCommentStatis = mapper.feedbackOfComment(baseSchoolIds, baseTrimesterId, baseSectionIds, dataSubjectIds);
        Map<Long , List<FeedbackCommentStatis>> commentMap = BeanUtils.toGroup(feedbackCommentStatis , "feedbackId");

        List<FeedbackSituationStatis> situationStatis = mapper.feedbackOfSituation(baseSchoolIds , baseTrimesterId , baseSectionIds ,dataSubjectIds);
        Map<Integer , List<FeedbackSituationStatis>> situationMap = BeanUtils.toGroup(situationStatis , "type");
        Map<Long , List<FeedbackSituationStatis>> tapMap = BeanUtils.toGroup(situationMap.get(FeedbackSituationEnums.type.behaviour.getValue()) , "feedbackId");
        Map<Long , List<FeedbackSituationStatis>> allMap = BeanUtils.toGroup(situationStatis, "feedbackId");

        for (int i = 0; i < feedbackStudentStatisList.size(); i++ ) {

            FeedbackStudentStatis fos = feedbackStudentStatisList.get(i);
            HSSFRow row = sheet2.createRow(i + 1);

            int c = 0;
            HSSFCell cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getSchoolName());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getBaseTrimester());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getBaseSection());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getLessonClassNum());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getLessonClassName());

            cell = row.createCell(c++);
            cell.setCellValue(LessonClassEnums.status.getNameByValue(fos.getStatus()));
            
            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getStudentNo());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getStudentName());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getFeedbackClass());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getFeedbackNum());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getZeroStatusNum());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getOneStatusNum());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getTwoStatusNum());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getCreateTime());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getCreateTeacher());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getClassTeacher());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            StringBuffer commentStr = new StringBuffer();
            if(commentMap.containsKey(fos.getFeedbackId())){
                List<FeedbackCommentStatis> commentStatis = commentMap.get(fos.getFeedbackId());
                if(commentStatis.size() > 0){
                    commentStatis.forEach(cs -> {
                        String comment = " ";
                        if(!StringUtils.isEmpty(cs.getContent())){
                            try {
                                comment = java.net.URLDecoder.decode(cs.getContent(), "utf-8");
                            } catch (UnsupportedEncodingException e) {
                                LogUtil.error(logger , "编码解码出错："+cs.getContent() , e);
                            }
                        }

                        commentStr.append(cs.getType() == FeedbackCommentEnums.userType.tea.getValue() ? "--教师":"家长");
                        commentStr.append("(" + cs.getCreateTime() + ") : ");
                        commentStr.append(comment + "\r\n");
                    });
                }
            }
            cell.setCellValue(commentStr.toString());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(null != fos.getIsPraise() && fos.getIsPraise() == 0 ? "是":"否");

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(null != fos.getIsExce() && fos.getIsExce() == 1 ? "是":"否");

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(tapMap.containsKey(fos.getFeedbackId()) ?
                    tapMap.get(fos.getFeedbackId()).stream().map(feedbackSituationStatis -> feedbackSituationStatis.getName()).collect(Collectors.joining(";"))
                    : "");

            final StringBuffer comment = new StringBuffer(" ");
            if(!CollectionUtils.isEmpty(allMap.get(fos.getFeedbackId()))){
                allMap.get(fos.getFeedbackId()).forEach(feedbackSituationStatis -> {
                    if(feedbackSituationStatis.getType() == FeedbackSituationEnums.type.content.getValue()){
                        try {
                            comment.append("(" + DateUtil.format(feedbackSituationStatis.getCreateTime() , DateUtil.yyyyMMddHHmmss) + ") "
                                    + java.net.URLDecoder.decode(feedbackSituationStatis.getSituation(), "utf-8") + "\n");
                        } catch (UnsupportedEncodingException e) {
                            LogUtil.error(logger , "编码解码出错："+fos.getSituation() , e);
                        }
                    }else if(feedbackSituationStatis.getType() == FeedbackSituationEnums.type.voice.getValue()){
                        comment.append("(" + DateUtil.format(feedbackSituationStatis.getCreateTime() , DateUtil.yyyyMMddHHmmss) + ") 该条反馈为语音\n");
                    }
                });
            }

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(comment.toString());

            cell = row.createCell(c++);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(fos.getIsRead() != null && fos.getIsRead() == 1 ? "是":"否");
        }

        return feedbackExcel;
    }

    @Override
    public FeedbackNameResp getFeedbackName(Long feedbackId) {
        return mapper.getFeedbackName(feedbackId);
    }

    @Override
    public void set2Read(Long feedbackId , Long userId) {

        Feedback f = this.selectByPrimaryKey(feedbackId);
        if(f == null || !f.getCreateUser().equals(userId)){
            return ;
        }
        feedbackCommentService.setIsReadByFeedback(feedbackId);
    }

    public void saveSituation(FeedBackEditReqVo vo, Feedback feedback , FeedbackClass feedbackClass){

        Date now = new Date();
        int img_sort = 1;
        int audio_sort = 1;
        List<FeedbackSituation> needOpreate = new ArrayList<>();

        //原本的图片
        if (vo.getPictures() != null && !vo.getPictures().equals("")) {
            for (String picture : vo.getPictures().split(";")) {
                if (!StringUtils.isEmpty(picture)) {
                    FeedbackSituation feedbackSituation = new FeedbackSituation();
                    feedbackSituation.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
                    feedbackSituation.setCreateTime(now);
                    feedbackSituation.setFeedbackId(feedback.getId());
                    feedbackSituation.setSort(img_sort++);
                    feedbackSituation.setId(Sequence.getSequence().nextId());
                    feedbackSituation.setType(FeedbackSituationEnums.type.picture.getValue());
                    if(picture.contains(",")){
                        String[] wetcats = picture.split(",");
                        if(wetcats.length < 2){
                            continue;
                        }
                        feedbackSituation.setWeixinMediaId(wetcats[0]);
                        feedbackSituation.setMedia(wetcats[1]);
                        feedbackSituation.setStatus(MediaOperateEnums.status.operating.getValue());
                        needOpreate.add(feedbackSituation);
                    }else {
                        feedbackSituation.setMedia(picture);
                        feedbackSituation.setStatus(MediaOperateEnums.status.needNot.getValue());
                    }
                    feedbackSituationService.insert(feedbackSituation);
                }
            }
        }
        //新增的图片
//        if (vo.getPictures() != null && vo.getPictures().length > 0) {
//            String[] new_urls = callableService.completionServiceCount(vo.getPictures() , "image");
//            for (String picture : new_urls) {
//                if (!StringUtils.isEmpty(picture)) {
//                    FeedbackSituation feedbackSituation = new FeedbackSituation();
//                    feedbackSituation.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
//                    feedbackSituation.setCreateTime(now);
//                    feedbackSituation.setFeedbackId(feedback.getId());
//                    feedbackSituation.setType(FeedbackSituationEnums.type.picture.getValue());
//                    feedbackSituation.setMedia(picture);
//                    feedbackSituation.setSort(img_sort++);
//                    feedbackSituation.setId(Sequence.getSequence().nextId());
//                    feedbackSituationService.insert(feedbackSituation);
//                }
//            }
//        }

        //原本的音频
        if (vo.getAudios() != null && !vo.getAudios().equals("")) {
            String[] audios = vo.getAudios().split(";");
            for (int i = 0 ; i < audios.length ; i++){
                String audio = audios[i];
                if (!StringUtils.isEmpty(audio)) {
                    FeedbackSituation feedbackSituation = new FeedbackSituation();
                    feedbackSituation.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
                    feedbackSituation.setCreateTime(now);
                    feedbackSituation.setFeedbackId(feedback.getId());
                    feedbackSituation.setType(FeedbackSituationEnums.type.voice.getValue());
                    feedbackSituation.setSort(audio_sort++);
                    feedbackSituation.setId(Sequence.getSequence().nextId());
                    if(audio.contains(",")){
                        String[] wetcats = audio.split(",");
                        if(wetcats.length < 2){
                            continue;
                        }
                        feedbackSituation.setWeixinMediaId(wetcats[0]);
                        feedbackSituation.setMedia(wetcats[1]);
                        feedbackSituation.setStatus(MediaOperateEnums.status.operating.getValue());
                        needOpreate.add(feedbackSituation);
                    }else {
                        feedbackSituation.setMedia(audio);
                        feedbackSituation.setStatus(MediaOperateEnums.status.needNot.getValue());
                    }
                    if(vo.getTimes() != null && vo.getTimes().length >= i+1 && vo.getTimes()[i] != null){
                        feedbackSituation.setTime(vo.getTimes()[i]);
                    }else {
                        feedbackSituation.setTime(0);
                    }
                    feedbackSituationService.insert(feedbackSituation);
                }
            }
        }

        //新增音频
//        if (vo.getAudios() != null && vo.getAudios().length > 0) {
//            String[] new_urls = callableService.completionServiceCount(vo.getAudios() , "voice");
//            for (String audio : new_urls) {
//                if (!StringUtils.isEmpty(audio)) {
//                    FeedbackSituation feedbackSituation = new FeedbackSituation();
//                    feedbackSituation.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
//                    feedbackSituation.setCreateTime(now);
//                    feedbackSituation.setFeedbackId(feedback.getId());
//                    feedbackSituation.setType(FeedbackSituationEnums.type.voice.getValue());
//                    feedbackSituation.setMedia(audio);
//                    feedbackSituation.setSort(audio_sort++);
//                    feedbackSituation.setId(Sequence.getSequence().nextId());
//                    feedbackSituationService.insert(feedbackSituation);
//                }
//            }
//        }


        //学习表现
        if (vo.getLabels() != null && vo.getLabels().length > 0) {
            int sort = 1;
            for (Long label : vo.getLabels()) {
                if (label != null) {
                    FeedbackSituation feedbackSituation = new FeedbackSituation();
                    feedbackSituation.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
                    feedbackSituation.setCreateTime(now);
                    feedbackSituation.setFeedbackId(feedback.getId());
                    feedbackSituation.setType(FeedbackSituationEnums.type.behaviour.getValue());
                    feedbackSituation.setSituationId(label);
                    feedbackSituation.setSort(sort++);
                    feedbackSituation.setId(Sequence.getSequence().nextId());
                    feedbackSituation.setStatus(MediaOperateEnums.status.needNot.getValue());
                    feedbackSituationService.insert(feedbackSituation);
                }
            }
        }

        //内容
        if (!StringUtils.isEmpty(vo.getContent())) {
            FeedbackSituation feedbackSituation = new FeedbackSituation();
            feedbackSituation.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
            feedbackSituation.setCreateTime(now);
            feedbackSituation.setFeedbackId(feedback.getId());
            feedbackSituation.setType(FeedbackSituationEnums.type.content.getValue());
            feedbackSituation.setSituation(vo.getContent());
            feedbackSituation.setSort(1);
            feedbackSituation.setId(Sequence.getSequence().nextId());
            feedbackSituation.setStatus(MediaOperateEnums.status.needNot.getValue());
            feedbackSituationService.insert(feedbackSituation);
        }

        //优秀学生
        if(vo.getIs_exce() != null && vo.getIs_exce()){

            FeedbackStudentExce feedbackStudentExce = new FeedbackStudentExce();
            feedbackStudentExce.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
            feedbackStudentExce.setCreateTime(now);
            feedbackStudentExce.setSendUserId(vo.getUserId());
            feedbackStudentExce.setFeedbackClassId(feedbackClass.getId());
            feedbackStudentExce.setStudentId(vo.getReceUserId());
            feedbackStudentExce.setId(Sequence.getSequence().nextId());
            feedbackStudentExce.setReason(vo.getReason());
            feedbackStudentExceService.insert(feedbackStudentExce);
        }

        //回复家长信息
        if(!StringUtils.isEmpty(vo.getComment())){
            FeedbackComment feedbackComment = new FeedbackComment();
            feedbackComment.setAuthOrganizationId(feedbackClass.getAuthOrganizationId());
            feedbackComment.setId(Sequence.getSequence().nextId());
            feedbackComment.setContent(vo.getComment());
            feedbackComment.setFeedbackId(feedback.getId());
            feedbackComment.setCreateTime(now);
            feedbackComment.setUserId(feedbackClass.getTeacherId());
            feedbackComment.setType(FeedbackCommentEnums.userType.tea.getValue());
            feedbackCommentService.insert(feedbackComment);
        }

        //将需要处理的图片、音频插入job表
        insert2Job(needOpreate , null);
    }

}